//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "TripleBandLinearOp.h"
using namespace Cephei::QL::Methods::Finitedifferences::Operators;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Methods/Finitedifferences/Meshers/FdmMesher.h>
#include <gen/QL/Methods/Finitedifferences/Operators/FdmLinearOp.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Methods::Finitedifferences::Meshers;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp::CTripleBandLinearOp (Cephei::QL::Methods::Finitedifferences::Operators::ITripleBandLinearOp^ m) : CFdmLinearOp(CTripleBandLinearOp::typeid)
{
    CTripleBandLinearOp^ _Cm;
    try
    {
#ifdef HANDLE
        _phTripleBandLinearOp = NULL;
#endif
        _Cm = safe_cast<CTripleBandLinearOp^> (m);
        _Cm->Lock();
        QuantLib::TripleBandLinearOp& _m = static_cast<QuantLib::TripleBandLinearOp&> (_Cm->GetReference ()); 
        _ppTripleBandLinearOp = new boost::shared_ptr<QuantLib::TripleBandLinearOp> (new QuantLib::TripleBandLinearOp ( _m ));
        SetFdmLinearOp (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppTripleBandLinearOp));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cm != nullptr) _Cm->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp::CTripleBandLinearOp (UInt64 direction, Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher) : CFdmLinearOp(CTripleBandLinearOp::typeid)
{
    CFdmMesher^ _Cmesher;
    try
    {
#ifdef HANDLE
        _phTripleBandLinearOp = NULL;
#endif
        QuantLib::Size _direction = (QuantLib::Size)ValueHelper::Convert (direction); //d
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        _ppTripleBandLinearOp = new boost::shared_ptr<QuantLib::TripleBandLinearOp> (new QuantLib::TripleBandLinearOp ( _direction,  _mesher ));
        SetFdmLinearOp (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppTripleBandLinearOp));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmesher != nullptr) _Cmesher->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp::CTripleBandLinearOp (boost::shared_ptr<QuantLib::TripleBandLinearOp>& childNative, Object^ owner) : CFdmLinearOp(CTripleBandLinearOp::typeid)
{
#ifdef HANDLE
	_phTripleBandLinearOp = NULL;
#endif
	_ppTripleBandLinearOp = &childNative;
    _ppFdmLinearOp = new boost::shared_ptr<QuantLib::FdmLinearOp> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppTripleBandLinearOp));
}
Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp::CTripleBandLinearOp (QuantLib::TripleBandLinearOp& childNative, Object^ owner) : CFdmLinearOp(CTripleBandLinearOp::typeid)
{
#ifdef HANDLE
	_phTripleBandLinearOp = NULL;
#endif
	_ppTripleBandLinearOp = new boost::shared_ptr<QuantLib::TripleBandLinearOp> (&childNative);
    _ppFdmLinearOp = new boost::shared_ptr<QuantLib::FdmLinearOp> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppTripleBandLinearOp));
    _TripleBandLinearOpOwner = owner;
    _FdmLinearOpOwner = owner;
}

Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp::CTripleBandLinearOp (CTripleBandLinearOp^ copy) : CFdmLinearOp(CTripleBandLinearOp::typeid)
{
#ifdef HANDLE
	_phTripleBandLinearOp = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppTripleBandLinearOp = new boost::shared_ptr<QuantLib::TripleBandLinearOp> (copy->GetShared());
        _ppFdmLinearOp = new boost::shared_ptr<QuantLib::FdmLinearOp> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppTripleBandLinearOp));
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp::CTripleBandLinearOp (PLATFORM::Type^ t) : CFdmLinearOp(CTripleBandLinearOp::typeid)
{
#ifdef HANDLE
	_phTripleBandLinearOp = NULL;
#endif
	if (!t->IsSubclassOf(CTripleBandLinearOp::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp::CTripleBandLinearOp (QuantLib::Handle<QuantLib::TripleBandLinearOp>& childNative, Object^ owner)  : CFdmLinearOp(CTripleBandLinearOp::typeid)
{
	_phTripleBandLinearOp = &childNative;
	_ppTripleBandLinearOp = &static_cast<boost::shared_ptr<QuantLib::TripleBandLinearOp>>(childNative.currentLink());
    _ppFdmLinearOp = new boost::shared_ptr<QuantLib::FdmLinearOp> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppTripleBandLinearOp));
    _TripleBandLinearOpOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp::CTripleBandLinearOp (QuantLib::Handle<QuantLib::TripleBandLinearOp> childNative)  : CFdmLinearOp(CTripleBandLinearOp::typeid)
{
	_phTripleBandLinearOp = &childNative;
	_ppTripleBandLinearOp = &static_cast<boost::shared_ptr<QuantLib::TripleBandLinearOp>>(childNative.currentLink());
    _ppFdmLinearOp = new boost::shared_ptr<QuantLib::FdmLinearOp> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppTripleBandLinearOp));
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp::CTripleBandLinearOp (QuantLib::TripleBandLinearOp childNative)  : CFdmLinearOp(CTripleBandLinearOp::typeid)
{
#ifdef HANDLE
	_phTripleBandLinearOp = NULL;
#endif
	_ppTripleBandLinearOp = new boost::shared_ptr<QuantLib::TripleBandLinearOp> (new QuantLib::TripleBandLinearOp (childNative));
    _ppFdmLinearOp = new boost::shared_ptr<QuantLib::FdmLinearOp> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppTripleBandLinearOp));
}
#endif

Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp::~CTripleBandLinearOp ()
{
    if (_ppTripleBandLinearOp != NULL)
    {
	    delete _ppTripleBandLinearOp;
        _ppTripleBandLinearOp = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp::!CTripleBandLinearOp ()
{
    if (_ppTripleBandLinearOp != NULL)
    {
	    delete _ppTripleBandLinearOp;
    }
}
QuantLib::TripleBandLinearOp& Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp::GetReference ()
{
    if (_ppTripleBandLinearOp == NULL) throw REFNEW NativeNullException ();
	return **_ppTripleBandLinearOp;
}
boost::shared_ptr<QuantLib::TripleBandLinearOp>& Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp::GetShared ()
{
    if (_ppTripleBandLinearOp == NULL) throw REFNEW NativeNullException ();
	return *_ppTripleBandLinearOp;
}
QuantLib::TripleBandLinearOp* Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp::GetPointer ()
{
    if (_ppTripleBandLinearOp == NULL) throw REFNEW NativeNullException ();
	return &**_ppTripleBandLinearOp;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::TripleBandLinearOp>& Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp::GetHandle ()
{
	if (_phTripleBandLinearOp == NULL)
	{
		_phTripleBandLinearOp = new Handle<QuantLib::TripleBandLinearOp> (*_ppTripleBandLinearOp);
	}
	return *_phTripleBandLinearOp;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp::HasNative () 
{
	return (_ppTripleBandLinearOp != NULL);
}

Cephei::QL::Methods::Finitedifferences::Operators::ITripleBandLinearOp^ Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp::Axpyb (Cephei::QL::Math::IArray^ a, Cephei::QL::Methods::Finitedifferences::Operators::ITripleBandLinearOp^ x, Cephei::QL::Methods::Finitedifferences::Operators::ITripleBandLinearOp^ y, Cephei::QL::Math::IArray^ b)
{
    CArray^ _Ca;
    CTripleBandLinearOp^ _Cx;
    CTripleBandLinearOp^ _Cy;
    CArray^ _Cb;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ca = safe_cast<CArray^> (a);
        _Ca->Lock();
        QuantLib::Array& _a = static_cast<QuantLib::Array&> (_Ca->GetReference ()); 
        _Cx = safe_cast<CTripleBandLinearOp^> (x);
        _Cx->Lock();
        QuantLib::TripleBandLinearOp& _x = static_cast<QuantLib::TripleBandLinearOp&> (_Cx->GetReference ()); 
        _Cy = safe_cast<CTripleBandLinearOp^> (y);
        _Cy->Lock();
        QuantLib::TripleBandLinearOp& _y = static_cast<QuantLib::TripleBandLinearOp&> (_Cy->GetReference ()); 
        _Cb = safe_cast<CArray^> (b);
        _Cb->Lock();
        QuantLib::Array& _b = static_cast<QuantLib::Array&> (_Cb->GetReference ()); 
    	(*_ppTripleBandLinearOp)->axpyb ( _a,  _x,  _y,  _b );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ca != nullptr) _Ca->Unlock();
        if (_Cx != nullptr) _Cx->Unlock();
        if (_Cy != nullptr) _Cy->Unlock();
        if (_Cb != nullptr) _Cb->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::ITripleBandLinearOp^ Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp::Swap (Cephei::QL::Methods::Finitedifferences::Operators::ITripleBandLinearOp^ m)
{
    CTripleBandLinearOp^ _Cm;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cm = safe_cast<CTripleBandLinearOp^> (m);
        _Cm->Lock();
        QuantLib::TripleBandLinearOp& _m = static_cast<QuantLib::TripleBandLinearOp&> (_Cm->GetReference ()); 
    	(*_ppTripleBandLinearOp)->swap ( _m );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cm != nullptr) _Cm->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Operators::ITripleBandLinearOp^ Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp_Factory::Create (Cephei::QL::Methods::Finitedifferences::Operators::ITripleBandLinearOp^ m)
{
    return REFNEW CTripleBandLinearOp ( m);
}
Cephei::QL::Methods::Finitedifferences::Operators::ITripleBandLinearOp^ Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp_Factory::Create (UInt64 direction, Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher)
{
    return REFNEW CTripleBandLinearOp ( direction,  mesher);
}
